package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.model.CDCEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author atguigu-mqx
 * @ClassName AlbumInfoCdcHandler
 * @description: TODO
 * @date 2024年10月08日
 * @version: 1.0
 */
@Component
@CanalTable("album_info")
public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity>{

    //  引入缓存的客户端
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(CDCEntity cdcEntity) {
        EntryHandler.super.insert(cdcEntity);
    }

    @Override
    public void update(CDCEntity before, CDCEntity after) {
        System.out.println("修改after:\t"+after.getId());
        System.out.println("修改before:\t"+before.getId());
        //  album:[]; 如果删除失败了，怎么保证?
        try {
            redisTemplate.delete("album_info:"+after.getId());
        } catch (Exception e) {
            // {"action":"update","id":"after.getId()"}
            //  sendMsg(exchange,routingkey,JSON.toJsonString()); ==> 监听的时候，重试删除操作； 记录异常消息表！
            throw new RuntimeException(e);
        }
        EntryHandler.super.update(before, after);
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        EntryHandler.super.delete(cdcEntity);
    }
}
